﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace MDIDemo.Modules
{
    public partial class HangHoa : DevExpress.XtraEditors.XtraForm
    {
        string sErr = "";
        int idThem = 16;
        int idSua = 17;
        int idXoa = 18;
        void SetPermission()
        {
            if (this.Parent.Parent is frmMain)
            {
                frmMain main = (frmMain)this.Parent.Parent;
                btnMThem.Visible = btnThem.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION);
                btnMSua.Visible = btnSua.Visible = frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
                btnMXoa.Visible = btnXoa.Visible = frmMain.IsPermis(idXoa, main.CurrentPermis.PERMISSION);
                btnMLuu.Visible = btnLuu.Visible = frmMain.IsPermis(idThem, main.CurrentPermis.PERMISSION) && frmMain.IsPermis(idSua, main.CurrentPermis.PERMISSION);
            }
        }
        public HangHoa()
        {
            InitializeComponent();

        }

        #region Nhom hang
        private void RefreshFormNhom(string p)
        {
            txtMaNhom.Text = p;
            txtTenNhom.Text = p;
        }
        private DTO.NHOMHANGInfo GetDataNhom()
        {
            return new DTO.NHOMHANGInfo(txtMaNhom.Text, txtTenNhom.Text);
        }
        private void EnabledFormNhom(bool p)
        {
            txtMaHang.Focus();
            txtMaNhom.Properties.ReadOnly = !p;
            txtTenNhom.Properties.ReadOnly = !p;
            btnXoa.Enabled = !p;
            btnThem.Enabled = !p;
            btnLuu.Enabled = p;
            btnXoa.Enabled = !p;
            btnSua.Enabled = !p;
            btnBoQua.Enabled = p;
            gridMaHang.Enabled = !p;
            gpMaHang.Enabled = !p;

        }
        private void SetDataNhom(DTO.NHOMHANGInfo inf)
        {
            txtMaNhom.Text = inf.NHOMHANG;
            txtTenNhom.Text = inf.TENNHOM;
            BUS.LOAIHANGControl ctr = new BUS.LOAIHANGControl();
            gridMaHang.DataSource = ctr.GetByNhom(inf.NHOMHANG, ref sErr);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            RefreshFormNhom("");
            EnabledFormNhom(true);
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            EnabledFormNhom(true);
            txtMaNhom.Properties.ReadOnly = true;
        }

        private void btnBoQua_Click(object sender, EventArgs e)
        {
            RefreshFormNhom("");
            EnabledFormNhom(false);
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            sErr = "";
            int[] currentRows = gvNhomHang.GetSelectedRows();
            if (currentRows != null && currentRows.Length > 0)
            {
                BUS.NHOMHANGControl ctr = new BUS.NHOMHANGControl();
                DTO.NHOMHANGInfo inf = new DTO.NHOMHANGInfo(gvNhomHang.GetDataRow(currentRows[0]));
                if (!ctr.IsUsed(inf.NHOMHANG))
                {
                    if (XtraMessageBox.Show("Bạn có thực sự muốn xóa", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        sErr = ctr.Delete(inf.NHOMHANG);
                        if (sErr == "")
                            XtraMessageBox.Show("Xóa thành công", "Thông báo");
                        else XtraMessageBox.Show(sErr, "Lỗi");
                        gridNhomHang.DataSource = ctr.GetAll(ref sErr);
                    }
                }
                else
                    XtraMessageBox.Show("Nhóm hàng đã được dùng, bạn không thể xóa", "Thông báo");
            }
        }
        private void gvNhomHang_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gvNhomHang.FocusedRowHandle > -1)
            {
                RefreshFormHang("");
                DTO.NHOMHANGInfo inf = new DTO.NHOMHANGInfo(gvNhomHang.GetDataRow(gvNhomHang.FocusedRowHandle));
                SetDataNhom(inf);
                gvMaHang_FocusedRowChanged(null, null);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            sErr = "";
            if (txtMaNhom.Text == "")
            {
                XtraMessageBox.Show("Vui lòng nhập mã nhóm!");
                return;
            }
            DTO.NHOMHANGInfo inf = GetDataNhom();
            BUS.NHOMHANGControl ctr = new BUS.NHOMHANGControl();
            if (txtMaNhom.Properties.ReadOnly)
                sErr = ctr.Update(inf);
            else ctr.Add(inf, ref sErr);
            if (sErr == "")
                XtraMessageBox.Show("Lưu thành công", "Thông báo");
            else XtraMessageBox.Show("Mã nhóm hàng đã tồn tại", "Lỗi");
            gridNhomHang.DataSource = ctr.GetAll(ref sErr);
            EnabledFormNhom(false);
        }
        #endregion

        private void RefreshFormHang(string p)
        {
            txtMaHang.Text = p;
            txtTenHang.Text = p;
            txtDonViTinh.EditValue = p;
            txtQuyCach.Text = p;
            txtGhiChu.Text = p;
        }

        private string GetNextID()
        {
            DTO.NHOMHANGInfo inf = new DTO.NHOMHANGInfo(gvNhomHang.GetFocusedDataRow());
            BUS.CommonControl ctr = new BUS.CommonControl();
            return ctr.GetItNextNo(inf.NHOMHANG);
        }
        private DTO.LOAIHANGInfo GetDataHang()
        {
            int[] currentRows = gvNhomHang.GetSelectedRows();
            if (currentRows != null && currentRows.Length > 0)
            {
                BUS.NHOMHANGControl ctr = new BUS.NHOMHANGControl();
                DTO.NHOMHANGInfo inf = new DTO.NHOMHANGInfo(gvNhomHang.GetDataRow(currentRows[0]));
                return new DTO.LOAIHANGInfo(txtMaHang.Text, "0001", inf.NHOMHANG, txtTenHang.Text, txtDonViTinh.SelectedValue, txtQuyCach.Text, txtGhiChu.Text, ckbVAT.Checked);
            }
            return null;

        }
        private void EnabledFormHang(bool p)
        {
            txtMaHang.Focus();
            txtMaHang.Properties.ReadOnly = !p;
            txtTenHang.Properties.ReadOnly = !p;
            txtDonViTinh.Properties.ReadOnly = !p;
            txtQuyCach.Properties.ReadOnly = !p;
            txtGhiChu.Properties.ReadOnly = !p;
            ckbVAT.Properties.ReadOnly = !p;
            btnMXoa.Enabled = !p;
            btnMThem.Enabled = !p;
            btnMLuu.Enabled = p;
            btnMXoa.Enabled = !p;
            btnMSua.Enabled = !p;
            btnMBoQua.Enabled = p;
            gridMaHang.Enabled = !p;
            gpNhomhang.Enabled = !p;

        }
        private void SetDataHang(DTO.LOAIHANGInfo inf)
        {
            txtMaHang.Text = inf.MAHANG;
            txtTenHang.Text = inf.TENHANG;
            txtDonViTinh.SelectedValue = inf.DVTINH;
            txtQuyCach.Text = inf.QUYCACH;
            txtGhiChu.Text = inf.GHICHU;
            ckbVAT.Checked = inf.NOTVAT;
        }

        private void HangHoa_Load(object sender, EventArgs e)
        {
            EnabledFormNhom(false);
            EnabledFormHang(false);
            BUS.DONVITINHControl ctrdvt = new BUS.DONVITINHControl();
            txtDonViTinh.DataSource = ctrdvt.GetAll(ref sErr);
            BUS.NHOMHANGControl ctr = new BUS.NHOMHANGControl();
            gridNhomHang.DataSource = ctr.GetAll(ref sErr);
            SetPermission();
        }

        private void btnMThem_Click(object sender, EventArgs e)
        {
            RefreshFormHang("");
            txtMaHang.Text = GetNextID();
            EnabledFormHang(true);
        }

        private void btnMSua_Click(object sender, EventArgs e)
        {
            EnabledFormHang(true);
            txtMaHang.Properties.ReadOnly = true;
        }

        private void btnMLuu_Click(object sender, EventArgs e)
        {
            sErr = "";
            if (txtMaNhom.Text == "")
            {
                XtraMessageBox.Show("Vui lòng nhập mã hàng!");
                return;
            }
            DTO.LOAIHANGInfo inf = GetDataHang();
            BUS.LOAIHANGControl ctr = new BUS.LOAIHANGControl();
            DTO.TONKHOInfo tkInf = new DTO.TONKHOInfo(inf.NHOMHANG, inf.MAHANG, inf.LOCATION, 0, 0, 0, 0, 0, 0, frmMain.__userID, frmMain.__userID, DateTime.Now, DateTime.Now);
            BUS.TONKHOControl tkctr = new BUS.TONKHOControl();
            if (txtMaHang.Properties.ReadOnly)
            {
                sErr = ctr.Update(inf);
            }
            else
            {
                if (!tkctr.IsExist(tkInf.NHOMHANG, tkInf.MAHANG))
                    tkctr.Add(tkInf, ref sErr);
                ctr.Add(inf, ref sErr);
            }
            if (sErr == "")
                XtraMessageBox.Show("Lưu thành công", "Thông báo");
            else XtraMessageBox.Show(sErr, "Lỗi");
            gvNhomHang_FocusedRowChanged(null, null);
            EnabledFormHang(false);
        }

        private void btnMBoQua_Click(object sender, EventArgs e)
        {
            RefreshFormHang("");
            EnabledFormHang(false);
        }

        private void btnMXoa_Click(object sender, EventArgs e)
        {
            sErr = "";
            int[] currentRows = gvMaHang.GetSelectedRows();
            if (currentRows != null && currentRows.Length > 0)
            {
                BUS.LOAIHANGControl ctr = new BUS.LOAIHANGControl();
                DTO.LOAIHANGInfo inf = new DTO.LOAIHANGInfo(gvMaHang.GetDataRow(currentRows[0]));
                if (!ctr.IsUsed(inf.MAHANG))
                {
                    if (XtraMessageBox.Show("Bạn có thực sự muốn xóa", "Thông báo", MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        BUS.TONKHOControl tkctr = new BUS.TONKHOControl();
                        sErr = tkctr.Delete(inf.NHOMHANG, inf.MAHANG);

                        if (sErr == "")
                        {
                            sErr = ctr.Delete(inf.MAHANG);
                            if (sErr == "")
                                XtraMessageBox.Show("Xóa thành công", "Thông báo");
                            else XtraMessageBox.Show(sErr, "Lỗi");
                            gridMaHang.DataSource = ctr.GetAll(ref sErr);
                        }
                    }
                }
                else
                    XtraMessageBox.Show("Loại hàng đã được dùng, bạn không thể xóa", "Thông báo");
            }

        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void HangHoa_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (btnBoQua.Enabled)
                    btnBoQua_Click(null, null);
                if (btnMBoQua.Enabled)
                    btnMBoQua_Click(null, null);
            }
            else if (e.KeyCode == Keys.F7)
            {
                if (btnLuu.Enabled)
                    btnLuu_Click(null, null);
                if (btnMLuu.Enabled)
                    btnMLuu_Click(null, null);
            }
            //else if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
            //{
            //    TextEdit txt = sender as TextEdit;
            //    if (txt != null) txt.Paste();
            //}
        }

        private void gridNhomHang_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                if (btnSua.Enabled)
                    btnSua_Click(null, null);
            }
            else if (e.KeyCode == Keys.N && e.Modifiers == Keys.Control)
            {
                if (btnThem.Enabled)
                    btnThem_Click(null, null);
            }
            else if (e.KeyCode == Keys.X && e.Modifiers == Keys.Control)
            {
                if (btnXoa.Enabled)
                    btnXoa_Click(null, null);
            }
        }

        private void gridMaHang_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                if (btnMSua.Enabled)
                    btnMSua_Click(null, null);
            }
            else if (e.KeyCode == Keys.N && e.Modifiers == Keys.Control)
            {
                if (btnMThem.Enabled)
                    btnMThem_Click(null, null);
            }
            else if (e.KeyCode == Keys.X && e.Modifiers == Keys.Control)
            {
                if (btnMXoa.Enabled)
                    btnMXoa_Click(null, null);
            }
        }

        private void gvMaHang_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gvMaHang.FocusedRowHandle > -1)
            {
                DTO.LOAIHANGInfo inf = new DTO.LOAIHANGInfo(gvMaHang.GetDataRow(gvMaHang.FocusedRowHandle));
                SetDataHang(inf);
            }
        }



    }
}